import {createRouter, createWebHistory} from 'vue-router'
import indexView from "@/views/index.vue";
import baseView from "@/views/layout/base.vue";
import {getLoginUser} from "@/api/userApi.js";
import {successCode} from "@/api/http.js";
import {userStore} from "@/stores/userStore.js";
import {adminUrl, portalUrl} from "@/api/const.js";

let routes = [
    {
        path: '/',
        redirect: '/index',
        component: baseView,
        children: [
            {
                path: "/index",
                name: 'index',
                alias: '首页',
                icon: 'icon-shouye',
                power: 99999,
                component: indexView
            },
            {
                path: "/user",
                name: 'user',
                alias: '用户管理',
                icon: 'icon-yonghuguanli',
                power: 99999,
                component: indexView
            },
            {
                path: '',
                redirect: '/category',
                alias: '分类管理',
                icon: 'icon-shouye',
                power: 9999999,
                children: [
                    {
                        path: "/category",
                        name: 'category',
                        alias: '商品分类管理',
                        icon: 'icon-shouye',
                        power: 99999,
                        component: indexView
                    },
                    {
                        path: "/project",
                        name: 'project',
                        alias: '商城专栏管理',
                        icon: 'icon-shouye',
                        power: 999999999,
                        component: indexView
                    }
                ]
            },
            {
                path: "/advertisement",
                name: 'advertisement',
                alias: '广告管理',
                icon: 'icon-guanggaoguanli',
                power: 9999999,
                component: indexView
            },
            {
                path: "/web",
                name: 'web',
                alias: '网站管理',
                icon: 'icon-wangzhanxinxi',
                power: 9999999,
                component: indexView
            },
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login/login.vue'),
        meta: {title: '登录'}
    }
]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: routes
})


// 全局守卫
router.beforeEach(async (to, from, next) => {
    let isUser = userStore().user;
    if (!isUser){
        let loginUser = await getLoginUser();
        if (successCode === loginUser.code) {
            userStore().setLoginUser(loginUser.data)
        }
        isUser = userStore().user;
    }
    let admin = userStore().admin;
    //判断是否有权限进入界面
    if (isUser && !admin) {
        // location.href = portalUrl;
        next();
    }
    if (to.path === "/login") {
        if (isUser) {
            next({path: '/index'});
        }
        // location.href = portalUrl + "/login?redirect=" + adminUrl;
        next();
    } else {
        if (!isUser) {
            // location.href = portalUrl + "/login?redirect=" + adminUrl;
            next("/login")
        }
        next();
    }
})

export default router
